package exttable.timetable.groupshchemapanel;

import dbutils.DbUtils;
import dbutils.StoredQueries;
import utils.Localisation;

import javax.swing.table.AbstractTableModel;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: alex
 * Date: 20.09.2010
 * Time: 21:39:01
 * To change this template use File | Settings | File Templates.
 */
public class GroupsSchemaTableModel extends AbstractTableModel {

    Vector<Vector> data = new Vector<Vector>();
    String[] columnNames = {Localisation.getMessage("table.column.groupid")
            , Localisation.getMessage("table.column.groupcode")
            , Localisation.getMessage("table.column.stardate")
            , Localisation.getMessage("table.column.phase")
            , Localisation.getMessage("table.column.inverstype")
            , Localisation.getMessage("table.column.shcema")
    };

    public GroupsSchemaTableModel() {
        initData();
    }


    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Vector v = data.get(rowIndex);
        return v.get(columnIndex);
    }

    private void initData() {

        try {
            Connection c = DbUtils.getDBConnectionGeneral();
            PreparedStatement ps = c.prepareStatement(StoredQueries.getQuery(StoredQueries.GROUPS_IN_SCHEMAS));
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Vector<String> row = new Vector<String>();
                row.add(rs.getString("id"));
                row.add(rs.getString("gcode"));
                row.add(rs.getString("StartDate"));
                row.add(rs.getString("phase"));
                row.add(rs.getString("typeName"));
                row.add(rs.getString("groupSchema"));
                data.add(row);
            }

        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
